package org.zpmis.common.utils;
import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTDecodeException;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.util.Date;

public class JwtUtil {

    private static final String encodeSecretKey = "XX#$%()(#*!()!KL<><MQLMNQNQJQKsdfkjsdrow32234545fdf>?N<:{LWPW";

    /**
     * token过期时间
     */
    private static final long EXPIRE_TIME = 1000 * 60 * 60 * 24 * 7;

    public static void main(String[] args) {
        String token = createToken("1234");
        System.out.println(token);
        String account = getAccount("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2MTg5NzQ0ODksImFjY291bnQiOiIxMjM0In0.py9mz0BE8ueC1JfTiW5I1OeNXsyT3-9LAVzBw9JbdT0");
        System.out.println(account);
    }

    /**
     * 生成token
     * @return
     */
    public static String createToken(String account) {
        try {
            Date date = new Date(System.currentTimeMillis() + EXPIRE_TIME);
            Algorithm algorithm = Algorithm.HMAC256(account + encodeSecretKey);
            return JWT.create()
                    .withExpiresAt(date)
                    .withClaim("account", account)
                    .sign(algorithm);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 校验token是否失效
     * @param token
     * @return
     */
    public static boolean checkToken(String token, String account) {
        try {
            Algorithm algorithm = Algorithm.HMAC256(account + encodeSecretKey);
            JWTVerifier verifier = JWT.require(algorithm)
                    .build();
            DecodedJWT jwt = verifier.verify(token);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 获取用户account
     * @param token
     * @return
     */
    public static String getAccount(String token){
        try {
            DecodedJWT jwt = JWT.decode(token);
            return jwt.getClaim("account").asString();
        } catch (JWTDecodeException e) {
            return null;
        }
    }
}
